//
//  SamllAdvertisementView.m
//  Tumour-Doctor
//
//  Created by xrz on 2017/3/17.
//  Copyright © 2017年 李林. All rights reserved.
//

#import "SmallAdvertisementView.h"
#import "TitleTipCell.h"
@interface SmallAdvertisementView ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>
{
    CGFloat currentWidth;
    NSInteger numberOfItems;//需要返回的item的个数
}

@property (nonatomic,strong) UICollectionView *collectionView;

@end

@implementation SmallAdvertisementView
/**
 懒加载
 */
-(UICollectionView *)collectionView
{
    if (_collectionView == nil)
    {
        UICollectionViewFlowLayout *flowLayout = [[UICollectionViewFlowLayout alloc] init];
        flowLayout.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(44, 0, MDK_SCREEN_WIDTH - 88, self.height) collectionViewLayout:flowLayout];
        _collectionView.backgroundColor = [UIColor clearColor];
        _collectionView.delegate = self;
        _collectionView.dataSource = self;
        _collectionView.showsHorizontalScrollIndicator = NO;
        _collectionView.scrollEnabled = NO;
        [_collectionView registerClass:[TitleTipCell class] forCellWithReuseIdentifier:@"cell"];
    }
    return _collectionView;
}

-(void)setTipString:(NSString *)tipString
{
    _tipString = tipString;
    currentWidth = [tipString boundingRectWithSize:CGSizeMake(1000, self.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine attributes:@{NSFontAttributeName :[UIFont systemFontOfSize:16.]} context:nil].size.width+10;
    //判断提示语的字符宽度
    if (currentWidth > self.collectionView.frame.size.width)
    {
        numberOfItems = 3;
        [self addTimer];
    }
    else
    {
        numberOfItems = 1;
    }
    [self.collectionView reloadData];
}

-(instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = HexColor(@"fdf2cf");
        [self createSubviews];
        
    }
    return self;
}


/**
 创建子视图
 */
- (void)createSubviews
{
    UIImageView *leftImage = [[UIImageView alloc] initWithFrame:CGRectMake(15, 10, 15, 15)];
    leftImage.backgroundColor = [UIColor clearColor];
//    leftImage.image = [UIImage imageNamed:@"icon_laba"];
    leftImage.image = [UIImage imageNamed:@"icon_tongzhi"];

    [self addSubview:leftImage];

    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    rightBtn.backgroundColor = [UIColor clearColor];
    rightBtn.frame = CGRectMake(MDK_SCREEN_WIDTH-44, 0, 44, self.height);
//    [rightBtn setImage:[UIImage imageNamed:@"ggl_x_close"] forState:UIControlStateNormal];
    [rightBtn setImage:[UIImage imageNamed:@"btn_gb"] forState:UIControlStateNormal];

    [rightBtn addTarget:self
                 action:@selector(closeBtnClickAction)
       forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:rightBtn];

    [self addSubview:self.collectionView];
}


#pragma mark - NSTimer
/**
 * 添加定时器
 */
- (void)addTimer
{
    _timer = [NSTimer scheduledTimerWithTimeInterval:0.02 target:self selector:@selector(scrollCollectionView) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:_timer forMode:NSRunLoopCommonModes];
}

/**
 * 移除定时器
 */
- (void)removeTimer
{
    if (_timer.isValid)
    {
        [_timer invalidate];
        _timer = nil;
    }
}

- (void)scrollCollectionView
{

    CGFloat x= self.collectionView.contentOffset.x;
    if (x < (self.collectionView.contentSize.width-MDK_SCREEN_WIDTH+87))
    {
        self.collectionView.contentOffset = CGPointMake(x+1, 0);
//        NSLog(@"%f",self.collectionView.contentSize.width-MDK_SCREEN_WIDTH+87-x);
    }
    else
    {
        [self.collectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] atScrollPosition:UICollectionViewScrollPositionLeft animated:NO];
        CGFloat socroll_x = currentWidth -MDK_SCREEN_WIDTH+88 ;
        self.collectionView.contentOffset = CGPointMake(socroll_x , 0);
    }
}


#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return numberOfItems;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    TitleTipCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cell" forIndexPath:indexPath];
    cell.name.text = self.tipString;
    cell.name.frame = CGRectMake(0, 0, currentWidth, 35);
    return cell;
}

#pragma mark - UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    return CGSizeMake(currentWidth, self.height);
}


#pragma mark - actions
- (void)closeBtnClickAction
{

    [self removeTimer];
    if (self.closeBtnClick)
    {
        self.closeBtnClick(self);
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
